package com.simon.springbootdemo.iot.collect.action;

import com.simon.springbootdemo.iot.collect.CollectException;
import com.simon.springbootdemo.iot.dto.CollectTaskDto;
import com.simon.springbootdemo.iot.entity.CollectDetail;
import com.simon.springbootdemo.iot.entity.Signal;
import org.springframework.util.StringUtils;

import java.util.function.Consumer;

/**
 * 采集动作
 */
public abstract class CollectAction {

    /**
     * 采集动作名称
     *
     * @return
     */
    public abstract String getName();

    /**
     * 采集动作说明
     *
     * @return
     */
    public abstract String getDesc();

    /**
     * 点位数据校验
     *
     * @param signal
     */
    public void validate(Signal signal) throws CollectException {
        String address = signal.getAddress();
        if (!StringUtils.hasText(address)) {
            throw new CollectException("点位[" + signal.getName() + "]没有指定地址");
        }
    }

    /**
     * 设备数据校验
     *
     * @param detail
     */
    public void validate(CollectDetail detail) throws CollectException {
        if (detail == null || detail.getDevice() == null) {
            throw new CollectException("参数[device]不能为null");
        }

        if (!StringUtils.hasText(detail.getDevice().getDeviceSn())) {
            throw new CollectException("未指定设备编号");
        }
    }

    // public void validate(IotDevice device, IotPoint point) throws CollectException {
    //     if (device == null || device.getDeviceId() == null) {
    //         throw new CollectException("参数[device]不能为null");
    //     }
    //
    //     if (!StringUtils.hasText(device.getDeviceSn())) {
    //         throw new CollectException("未指定设备编号");
    //     }
    // }

    /**
     * 执行调用
     *
     * @param taskDto 任务
     * @param detail
     * @param signal
     * @param call
     */
    public void exec(CollectTaskDto taskDto, CollectDetail detail, Signal signal, Consumer<String> call) throws CollectException {
        doExec(taskDto, detail, signal, call);
    }

    protected abstract void doExec(CollectTaskDto taskDto, CollectDetail detail, Signal signal, Consumer<String> call);

    // protected abstract void doAction(IotDevice device, IotPoint point, Consumer<String> cb);
    //
    // public void action(IotDevice device, IotPoint point, Consumer<String> cb) {
    //     doAction(device, point, cb);
    // }
    // public void action(IotDevice device, IotPoint point, Consumer<String> cb);
}
